﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Work.Hardware;

namespace FreesenseSubAIS.Core
{
    /// <summary>
    /// CameraLink相机采集卡相机
    /// </summary>
    internal class CameraEuresys : ICameraAdapter
    {
        private EuresysCamera _camera;
        public string CameraName { get; private set; }

        public bool CameraState { get; private set; }
        public int Index;

        public CameraEuresys(int index)
        {
            Index = index;
        }

        public bool SetExprosetime(double extime)
        {
            if (!CameraState || !(extime > 14) || !(extime < 7000000)) return false;
            _camera.SetExposureTime(extime);
            return true;
        }

        public bool OneShot()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.OneShot();
                flag = true;
            }
            return flag;
        }

        public bool ContinueShot()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.StartContinuouShot();//1-3不要设置
                flag = true;
            }
            return flag;
        }

        public bool StopShot()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.StopContinuouShot();
                flag = true;
            }
            return flag;
        }

        public bool Close()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.Close();
                flag = true;
            }
            return flag;
        }

        public double GetExprosetime()
        {
            double extime = 0;
            if (CameraState)
            {
                extime = _camera.GetExposureTime();
            }
            return extime;
        }

        public double GetErrorCount()
        {
            double errorcount = 0;
            return errorcount;
        }

        public event EventHandler<Bitmap> ImageReady;

        public async Task Init()
        {
            try
            {
                await Task.Run(() =>
               {
                   _camera = new EuresysCamera((uint)Index);
                   _camera.FrameReady += _camera_FrameReady;
                   CameraState = true;
               });
            }
            catch (Exception ex)
            {
                CameraState = false;
                throw new ArgumentException(ex.ToString());
            }
            await Task.Delay(100);
        }

        private void _camera_FrameReady(Bitmap bitmap)
        {
            if (ImageReady != null)
                ImageReady(CameraName, bitmap);
        }

        public bool AquireBitmap()
        {
            return OneShot();
        }

        public bool AquireBitmap(string mapname)
        {
            return OneShot();
        }

        public bool AquireBitmap(int mapindex)
        {
            return OneShot();
        }
    }
}
